import { JXDef } from "../../../conventions/JXCommon";
import { GCtrl } from "../../../Core/GCtrl";
import GViewBase from "../../../Core/GView/GViewBase";
import ShaderUtil from "../../../Shaders/Manager/ShaderUtil";
import { PointInfo, RedpointEffect, RPointMask } from "../../Common/Define";
import { L } from "../../Common/Language";
import TimeUtil from "../../Common/TimeUtils";
import { VIEW_ID } from "../../Common/UI";
import { Res } from "../../Common/UIResources";
import { JXLocales } from "../../Common/Zh";
import GameMgr from "../../Logic/GameMgr";
import ItemSignIn from "./ItemSignIn";


const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/SignInCtrl')

export default class SignInCtrl extends GViewBase {
    @property(cc.Sprite) sign7thImg: cc.Sprite = null;
    @property(cc.Sprite) heroImg: cc.Sprite = null;

    @property(cc.Label) lb_ct: cc.Label = null;
    @property(cc.Label) lb_day: cc.Label = null;
    @property(cc.Node) Signed: cc.Node = null;
    @property(cc.Node) signBtn: cc.Node = null;
    @property(cc.Node) adSiBtn: cc.Node = null;

    @property(cc.Node) signBtnArr: cc.Node[] = [];
    @property(cc.Node) items: cc.Node[] = [];
    private signCount: number = 0;
    private officerLv: number = 0;
    private signOfficerLv: number = 0;
    awardDatas: SSignInDataRaw[] = null;
    onGLoad() {
        this.getSignLv();
    }
    onGStart() {
        this.getSignAwardData();
        // console.log(this.awardDatas);
        // this.win.maskNode.opacity = 225;
        this.initSignAward();
        this.initSignState();
        this.initLeftTime();
        // AudioMgr.Ins().playEffect(Res.audio.sign);
        this.setRedPoint();
        GCtrl.ES.on(GCtrl.GTimerSecondEventMsg, this, this.initLeftTime.bind(this));
    }

    protected setRedPoint() {
        let isPoint = GameMgr.userMgr.checkSignRedPoint();
        GameMgr.redMgr.setRPointView(this.adSiBtn, this.adSiBtn, isPoint, RedpointEffect.REDPOINT);
        GameMgr.redMgr.setRPointView(this.signBtn, this.signBtn, isPoint, RedpointEffect.REDPOINT);
    }

    /**获取签到奖励数据 */
    public getSignAwardData() {
        this.awardDatas = GameMgr.signInData.getSignByLv(this.signOfficerLv);
    }

    /**初始化对应图标的签到奖励 */
    public initSignAward() {
        let awardDatas = this.awardDatas;
        for (let i = 0; i < this.items.length; i++) {
            let item_sign = this.items[i].getComponent(ItemSignIn);
            if (i < this.items.length - 1) {
                item_sign.initTextureWithData(awardDatas[i]);
            } else {
                // console.log("awardDatas[i]", awardDatas[i]);

                this.assetImpl.spriteFrame(this.heroImg, Res.texture.hero.hero + awardDatas[i].icon);
                this.assetImpl.spriteAtlasFrame(this.sign7thImg, Res.texture.views.sign, 'sign_' + awardDatas[i].icon)
            }
            item_sign.init();
        }
    }

    /**根据官爵等级获取签到奖励 */
    private getSignLv() {
        this.officerLv = GameMgr.lUserData.officerLv;
        this.signOfficerLv = GameMgr.lUserData.signOfficerLv;
        let arr = [];
        let lv = -1;
        this.signCount = GameMgr.lUserData.getSignCount();//连续签到时间

        //已签到完7天且第2次签到不到是同天的话就判定为第8天开始又一次轮回
        if (this.signCount == 7 && !GameMgr.lUserData.todayIsSignIn()) {
            this.signCount = 0;
            GameMgr.lUserData.setSignCount(this.signCount);
        }

        if (this.signCount == 0 || this.signCount == 7) {
            GameMgr.signInData.data.forEach((value: SSignInDataRaw, index) => {
                if (lv < value.lv) {
                    lv = value.lv;
                    arr.push(lv);
                    // console.log('arr', arr);
                }
            })
            arr.forEach((value, index) => {
                if (value == this.officerLv) {
                    this.signOfficerLv = value;
                } else if (this.officerLv > arr.length - 1) {
                    this.signOfficerLv = arr[arr.length - 1];
                }
            })
            GameMgr.lUserData.signOfficerLv = this.signOfficerLv;
        }

    }

    /**签到状态 */
    public initSignState() {
        let signCount = GameMgr.lUserData.getSignCount();//连续签到时间
        let tadayIndex = GameMgr.lUserData.getSignTodayIndex();//连续签到时间 是否同一天
        for (let i = 0; i < JXDef.System.SIGN_ROUND; i++) {
            let item_sign = this.items[i].getComponent(ItemSignIn);
            if (i < signCount) {
                item_sign.haveSigned();
            } else {
                if (i > signCount) {
                    item_sign.hideSigned();
                } else {
                    item_sign.recoverySigned();
                }
            }
            item_sign.showTodayToday(i == tadayIndex);
        }

        if (tadayIndex) {
            this.Signed.active = true;
            ShaderUtil.gray(this.signBtnArr[0]);
            if (GameMgr.lUserData.doubleSign()) {
                ShaderUtil.gray(this.signBtnArr[1]);
            }
        } else {
            this.Signed.active = false;
        }
    }

    /**全局时间 */
    protected initLeftTime() {
        var timenow = new Date(GCtrl.now);
        var second = timenow.getHours() * 3600; //小时
        second += timenow.getMinutes() * 60;//分钟
        second += timenow.getSeconds(); //秒
        var remain = 86400 - second; //剩余秒
        this.lb_ct.string = TimeUtil.formatClock(remain, "hh:mm:ss");
    }

    /**签到奖励 */
    protected recAward(isDouble) {
        let signCount = GameMgr.lUserData.getSignCount();

        let todayAward = null;
        let award = null;
        if (GameMgr.lUserData.todayIsSignIn()) {
            todayAward = GameMgr.signInData.getRaw<SSignInDataRaw>(signCount);
            award = new Array(...todayAward.award);
        } else {
            todayAward = GameMgr.signInData.getRaw<SSignInDataRaw>(signCount + 1);
            award = new Array(...todayAward.award);
        }


        if (isDouble) {
            award.forEach((value, index) => {
                value[2] *= 2;
            })
            ShaderUtil.gray(this.signBtnArr[1]);
        }
        this.Signed.active = true;
        ShaderUtil.gray(this.signBtnArr[0]);

        // console.log('签到奖励', todayAward, award);
        GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, award);
        if (GameMgr.lUserData.todayIsSignIn()) {

        } else {
            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.signCtrl, { msg: L(JXLocales.tower.sign, signCount + 1) })
            GameMgr.lUserData.signIn();
        }

        this.initSignState();
    }

    /**点击签到奖励 */
    protected onSignClick() {
        // console.log('点击签到奖励');
        if (GameMgr.lUserData.todayIsSignIn()) {
            return;
        }
        // GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.sign)
        this.recAward(false);
        this.setRedPoint();
        GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Sign, false);
        // GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.signCtrl, { msg: PointInfo.sign_norl })
    }

    protected onSignAdClick() {
        if (GameMgr.lUserData.doubleSign()) {
            return;
        }
        GameMgr.sdkMgr.watchAd(() => {
            this.recAward(true);
            this.setRedPoint();
            GameMgr.lUserData.signDouble();
            GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Sign, false);
            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.signCtrl, { msg: PointInfo.sign_ad })
        })
    }

    close() {
        this.onClose();
    }
}   